package it.okkam.geosparql;

import it.okkam.strutturedati.Extent;
import it.okkam.utility.UtilityQuery;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;

public class GeoGetExtentComuni {
	
	Logger log = LoggerFactory.getLogger(GeoGetExtentComuni.class);
	
	public HashMap<String, Extent> getExtentsComuni(Model modelExtent){
		UtilityQuery utq = new UtilityQuery();
		GeoStringQuery geos = new GeoStringQuery();

		String queryString = geos.getStringQueryExtentComuni();
		
		com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
		QueryExecution qexec = QueryExecutionFactory.create(query, modelExtent);
        ResultSet results = qexec.execSelect();
        HashMap<String, Extent> extents = new HashMap<String, Extent>();
        Extent ext;
        for (; results.hasNext();) {
        	QuerySolution record = results.next();
        	String codiceComune = record.get("codiceComune").toString();
        	String extentMin = record.get("extentMin").toString();
        	String extentMax = record.get("extentMax").toString();
        	String extentMinLat = record.get("extentMinLat").toString();
        	String extentMinLong = record.get("extentMinLong").toString();
        	String extentMaxLat = record.get("extentMaxLat").toString();
        	String extentMaxLong = record.get("extentMaxLong").toString();
        	Extent extentQuery = new Extent(); 	
        	extentQuery.setxMin(Double.parseDouble(utq.getCoordinata(extentMinLat)));
        	extentQuery.setyMin(Double.parseDouble(utq.getCoordinata(extentMinLong)));
        	extentQuery.setxMax(Double.parseDouble(utq.getCoordinata(extentMaxLat)));
        	extentQuery.setyMax(Double.parseDouble(utq.getCoordinata(extentMaxLong)));      	
        	extents.put(codiceComune, extentQuery);
  		  
        }

        log.info(utq.getData()+" Query Extent Comuni done!");
		return extents;
	}
	
	
}
